<template>
    <div class="Box">
        <div class="left">
            <span class="total"> {{ props.prefix + props.total }}</span>
            <span class="label">
                <span class="label">
                    <slot name='title'></slot>
                </span>
            </span>
            <div>
                <span>{{ props.start_t }}</span>
                <span>{{ props.yesdayNum }}</span>
                <span>至</span>
                <span>{{ props.end_t }}</span>
                <span>{{ props.todayNum }}</span>
                <p>
                    <el-icon v-if="props.todayNum >= props.yesdayNum" :color="props.upColor"><Top /></el-icon>
                    <el-icon v-else :color="props.downColor"><Bottom /></el-icon>
                    <span :style="props.todayNum >= props.yesdayNum ? 'color:' + props.upColor : 'color:' + props.downColor">{{ rate }}% </span>
                </p>
            </div>
        </div>
        <div class="right">
            <div :style="{ 'background-color': props.iconBack }">
                <component :is="props.icon" class="icon"></component>
            </div>
        </div>
    </div> 
</template>

<script lang="ts" setup>
import { computed,withDefaults,defineProps } from 'vue'

interface Props{
    total: number
    todayNum: number
    yesdayNum: number
    iconBack: string
    icon: string
    upColor?: string   //增长率提升时的颜色
    downColor?: string //增长率下降时的颜色
    prefix?: string     //total前缀
    start_t?: string
    end_t?: string
}
const props = withDefaults(defineProps<Props>(),{
    upColor: '#67C23A',
    downColor: '#F56C6C',
    prefix: '',
    start_t: '昨天',
    end_t: '今天'
})
const rate = computed(() => {
    let diff = props.todayNum - props.yesdayNum
    if (diff > 0 && props.yesdayNum !== 0) {
        return Math.floor(diff / props.yesdayNum * 100)
    } else if (diff === 0) {
        return 0
    } else if (props.yesdayNum === 0) {
        console.log('进不来了');
        return props.todayNum * 100
    } else {
        return Math.floor(Math.abs(diff) / props.yesdayNum * 100)
    }
})
</script>

<style lang="scss" scoped>
.Box{
    width: 100%;
    background: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 8px;
    padding: 20px 30px;
    box-sizing: border-box;
    box-shadow: var(--el-box-shadow);
    user-select: none;
}
.left{
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    gap: 5px;
    & div:nth-child(3){
        display: flex;
        align-items: center;
        color: #61666D;
        font-size: 13px;
    }
    & div:nth-child(3) span:nth-child(3),span:nth-child(4){
        margin-left: 15px;
    }
    & div:nth-child(3) span:nth-child(2),span:nth-child(5){
        margin-left: 10px;
        font-size: 16px;
        color: #213547;
        font-weight: 600;
    }
    & div:nth-child(3) p:last-child{
        @include flexRowCenter;
        font-size: 15px;
        margin-left: 10px;
    }
    & div:nth-child(3) p:last-child span:last-child{
        margin-left: 0;
    }
}
.total{
    font-size: 20px;
    font-weight: 600;
}
.label{
    color: #61666D;
    font-size: 13px;
}
.right{
    @include flexRowCenter;
    & div:nth-child(1){
        position: relative;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background: coral;
        @include flexRowCenter;
        overflow: hidden;
    }
    & div:nth-child(1)::after{
        content: '';
        position: absolute;
        background: rgb(255, 255, 255,0.2);
        width: 10px;
        height: 100px;
        rotate: 45deg;
        left: 20px;
        top: -10px;
        z-index: 99;
    }
    & div:nth-child(1)::before{
        content: '';
        position: absolute;
        background: rgb(255, 255, 255,0.2);
        width: 10px;
        height: 100px;
        rotate: 45deg;
        left: 0px;
        top: -15px;
        z-index: 99;
    }
}
.icon{
    width: 30px;
    height: 30px;
    color: white;
}
</style>